package com.opal.jx.controller;

import com.opal.consts.WSC;
import com.opal.domain.R;
import com.opal.jx.entity.JxAssessReception;
import com.opal.jx.entity.table.JxAssessPzcj;
import com.opal.jx.exception.JxAssessException;
import com.opal.jx.service.JxPZCJService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.UnexpectedRollbackException;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 绩效考核项目的数据
 */
@Controller
@RequestMapping("jx/pzcj")
public class JxPZCJController {

    @Autowired
    private JxPZCJService jxPZCJService;

    /**
     * 配置车间开料员页面的【提交】触发此接口
     * @param jxAssessPzcjReception
     * @return
     */
    @PostMapping("addKaiLiaoYuanAssessData")
    @ResponseBody
    public Object addKaiLiaoYuanAssessData(@RequestBody JxAssessReception jxAssessPzcjReception){  //属性名随意名称
        R r = jxPZCJService.addKaiLiaoYuanAssessDataService(jxAssessPzcjReception);
        return r;
    }

    /**
     * 配置车间新开料员页面的【提交】触发此接口
     * @param jxAssessPzcjReception
     * @return
     */
    @PostMapping("addNewKaiLiaoYuanAssessData")
    @ResponseBody
    public Object addNewKaiLiaoYuanAssessData(@RequestBody JxAssessReception jxAssessPzcjReception){  //属性名随意名称
        R r = jxPZCJService.addNewKaiLiaoYuanAssessDataService(jxAssessPzcjReception);
        return r;
    }

    /**
     * 配置车间开料员管理人员页面【提交】触发此接口
     * @param jxAssessPzcjReception
     * @return
     */
    @PostMapping("addManagersAssessData")
    @ResponseBody
    public Object addManagersAssessData(@RequestBody JxAssessReception jxAssessPzcjReception){
        R r = jxPZCJService.addManagersAssessDataService(jxAssessPzcjReception);
        return r;
    }

    /**
     * 配置车间辅助人员页面【提交】触发此接口
     * @param jxAssessPzcjReception
     * @return
     */
    @PostMapping("addFuZhuRenYuanAssessData")
    @ResponseBody
    public Object addFuZhuRenYuanAssessData(@RequestBody JxAssessReception jxAssessPzcjReception){
        R r = jxPZCJService.addFuZhuRenYuanAssessDataService(jxAssessPzcjReception);
        return r;
    }

    /**
     * 配置车间培训组长页面【提交】触发此接口
     * @param jxAssessPzcjReception
     * @return
     */
    @PostMapping("addPeiXunZuZhangAssessData")
    @ResponseBody
    public Object addPeiXunZuZhangAssessData(@RequestBody JxAssessReception jxAssessPzcjReception){
        R r = jxPZCJService.addPeiXunZuZhangAssessDataService(jxAssessPzcjReception);
        return r;
    }

    /**
     * 培训组长中【班组生产效率】内容改变函数内触发此接口
     * @param jxAssessPzcj  根据当前行的班组/岗位名称/年份、月份来获取数据库中的所有新开料员的人数
     * @return
     */
    @RequestMapping("selectJxEmpGroupNum")
    @ResponseBody
    public Object selectJxEmpGroupNum(JxAssessPzcj jxAssessPzcj){
        List<JxAssessPzcj> jxAssessPzcjs = jxPZCJService.selectJxAssessPzcjByCondition(jxAssessPzcj);
        Map<String, Integer> map = new HashMap<>();
        int a = 0,b=0,c=0;
        for (JxAssessPzcj j:jxAssessPzcjs){
            Integer entryMonth = j.getEntryMonth();
            switch (entryMonth){
                case 1:
                    a++;
                    break;
                case 2:
                    b++;
                    break;
                case 3:
                    c++;
                    break;
            }
        }
        map.put("a", a);
        map.put("b", b);
        map.put("c", c);
        return map;
    }

    /**
     * 配置车间称料房主管页面【提交】触发此接口
     * @param jxAssessPzcjReception
     * @return
     */
    @RequestMapping("addChengLiaoAssessData")
    @ResponseBody
    public Object addChengLiaoAssessData(@RequestBody JxAssessReception jxAssessPzcjReception){
        R r = jxPZCJService.addChengLiaoAssessDataService(jxAssessPzcjReception);
        return r;
    }

    /**
     * 处理异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = RuntimeException.class) //手动调用了事务回滚发生的异常
    @ResponseBody
    public Object RuntimeExceptionController(Exception e){
        if (e instanceof JxAssessException){
            return new R(WSC.PRECONDITIONFAILED,e.getMessage());
        }else {
            return new R(WSC.REQUEST_ERROR,"提交失败：系统维护中，请销后再试！");
        }

    }

    /**
     * 处理手动回滚异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = UnexpectedRollbackException.class) //手动调用了事务回滚发生的异常
    @ResponseBody
    public Object nullException(Exception e){

        return new R(WSC.PRECONDITIONFAILED,"我手动回滚了啊");
    }
}
